# Author : ZZH
# Date : 2025/5/27
import cv2
from .beautify import custom_plot


def process_frame(frame, result, args, display_width, display_height, beautify_params):
    """
    处理并标注视频帧

    :param frame: 原始视频帧 (numpy.ndarray)
    :param result: YOLO检测结果 (Results对象)
    :param args: 命令行参数
    :param display_width: 显示宽度
    :param display_height: 显示高度
    :param beautify_params: 美化参数字典
    :return: 处理后的标注帧
    """
    # 自定义美化模式
    if args.beautify:
        annotated_frame = custom_plot(
            frame,
            boxes=result.boxes.xyxy.cpu().numpy(),
            confs=result.boxes.conf.cpu().numpy(),
            labels=[result.names[int(cls)] for cls in result.boxes.cls.cpu().numpy()],
            **beautify_params
        )
    # 默认YOLO标注模式
    else:
        try:
            # 尝试使用指定线宽参数
            annotated_frame = result.plot(line_width=beautify_params["line_width"])
        except Exception as e:
            print(f"Warning: YOLO line_width may not be supported: {e}")
            annotated_frame = result.plot()

    # 统一缩放至显示尺寸
    annotated_frame = cv2.resize(
        annotated_frame, (display_width, display_height))
    return annotated_frame
